<!DOCTYPE html>
<html>
    <head>
        <title>Test close the key session associated with the object when the page is inaccessible</title>
        <script src="../../resources/testharness.js"></script>
        <script src="../../resources/testharnessreport.js"></script>
    </head>
    <body>
        <script>
            var iframe;

            function load_iframe(src) {
                return new Promise(function(resolve) {
                    iframe = document.createElement('iframe');
                    iframe.onload = function() { resolve(iframe); };
                    iframe.src = src;
                    document.documentElement.appendChild(iframe);
                });
            }

            function wait_for_message() {
                return new Promise(function(resolve) {
                    self.addEventListener('message', function listener(e) {
                        if (e.data.comm == 'init-completed') {
                            iframe.contentWindow.postMessage({comm: 'close'}, '*');
                        } else if (e.data.comm == 'close-received') {
                            iframe.remove();
                            window.postMessage({result: 'finished'}, '*');
                        } else {
                            resolve(e.data);
                            self.removeEventListener('message', listener);
                        }
                    });
                });
            }

            async_test(function(test) {
               load_iframe("../resources/encrypted-media-session-close-and-context-destroyed-iframe.html").then(function(iframe) {
                    iframe.contentWindow.postMessage({comm: 'init'}, '*');
                    return wait_for_message();
                }).then(function(message) {
                    assert_equals(message.result, 'finished');
                    test.done();
                });
            }, 'Test close the key session associated with the object when the page is inaccessible');
        </script>
    </body>
</html>
